package com.example.yonghucenter.controller;
import com.example.yonghucenter.common.BaseResponse;
import com.example.yonghucenter.common.ErrorCode;
import com.example.yonghucenter.common.ResultUtils;
import com.example.yonghucenter.domain.dto.UserDTO;
import com.example.yonghucenter.domain.dto.UserLoginDTO;
import com.example.yonghucenter.domain.po.User;
import com.example.yonghucenter.exception.BusinessException;
import com.example.yonghucenter.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import static com.example.yonghucenter.constent.UserConstent.ADMIN_ROLE;
import static com.example.yonghucenter.constent.UserConstent.userLogin;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(String userAccount, String userPassword, String checkPassword) {
       long  result = userService.userRegister(userAccount, userPassword, checkPassword);
     return ResultUtils.success(result);
    }
    /**
     * 获取当前登录用户
     */
    @GetMapping("/currentUser")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        Object userObject = request.getSession().getAttribute(userLogin);
        if (userObject == null){
            return new BaseResponse<>(1,null,"当前用户不存在");
        }
        User user = (User) userObject;
        Long userId = user.getId();

        User user1 = userService.getById(userId);

        return  ResultUtils.success(user1);
    }
    /**
     * 用户登录
     */

    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }
    /**
     * 用户注销
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (session == null) {
           return ResultUtils.success(true);
        }
        session.removeAttribute(userLogin);
        return ResultUtils.success(true);
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> list(HttpServletRequest request) {
        //1.查询是否为管理员
     //isAdmin(request);
     List<User> listuser = userService.list();
        List<User> list = new ArrayList<>();
        //用户脱敏
        for (User user1 : listuser) {
            user1.setUserPassword(null);
            list.add(user1);
        }
        return ResultUtils.success(list);
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> delete(Integer id, HttpServletRequest request) {
        //1.查询是否为管理员
     isAdmin(request);
     Boolean result = userService.removeById(id);
     return ResultUtils.success(result);
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    public boolean update(User user, HttpServletRequest request, @RequestBody UserDTO userDTO) {
        //1.查询是否为管理员
     isAdmin(request);
     // 2. 修改用户
        User user2 = new User();
        user2.setUsername(userDTO.getUsername());
        user2.setUserAccount(userDTO.getUserAccount());
        user2.setAvatarUrl(userDTO.getAvatarUrl());
        user2.setGender(userDTO.getGender());
        user2.setPhone(userDTO.getPhone());
        user2.setEmail(userDTO.getEmail());
        user2.setUserStatus(userDTO.getUserStatus());
        user2.setUserRole(userDTO.getUserRole());
        return userService.updateById(user);
    }
 private void isAdmin(HttpServletRequest request) {
  Object userObject = request.getSession().getAttribute(userLogin);
  if (userObject == null) {
    throw new BusinessException(ErrorCode.NO_AUTH);
  }
  User user1 = (User) userObject;
  if (!user1.getUserRole().equals(ADMIN_ROLE)) {
      throw new RuntimeException("无管理员权限");
  }
 }


}
